//
//  HourMinuteDatePickerView.swift
//  UI Single
//
//  Created by vivi on 6/5/16.
//  Copyright © 2016 vivi. All rights reserved.
//

import UIKit

enum HourMinuteDatePickerViewAmPmType {
    case AM
    case PM
}

class HourMinuteDatePickerView: CustomDatePickerBaseView {
    
    var hour_12: Int {
        get {
            let hourText = self.tableView1.currentText
            return Int(hourText.substringToIndex(hourText.startIndex.advancedBy(2)))!
        }
        set {
           setHour12(newValue, animated: false) 
        }
    }
    
    var minute: Int {
        get {
            let minuteText = self.tableView2.currentText
            return Int(minuteText.substringToIndex(minuteText.startIndex.advancedBy(2)))!
        }
        set {
            setMinute(newValue, animated: false)
        }
    }
    
    var amOrPm: HourMinuteDatePickerViewAmPmType {
        get {
            return self.tableView3.currentText == "am" ? .AM : .PM
        }
        set {
            setAmPm(amOrPm, animated: false)
        }
    }
    
    override func configureInit() {
        super.configureInit()
        
        self.tableView1.texts = (1...12).map{String.init(format: "%02d时", $0)}
        self.tableView2.texts = (0...59).map{String.init(format: "%02d分", $0)}
        self.tableView3.texts = ["am", "pm", "am", "pm"]
    }
    
    func setHour12(hour: Int, animated: Bool) {
        self.tableView1.setCurrentTextTo(String.init(format: "%02d时", hour), animated: animated)
    }
    
    func setMinute(minute: Int, animated: Bool) {
        self.tableView2.setCurrentTextTo(String.init(format: "%02d分", minute), animated: animated)
    }
    
    func setAmPm(amOrPm: HourMinuteDatePickerViewAmPmType, animated: Bool) {
        switch amOrPm {
        case .AM: 
            self.tableView3.setCurrentTextTo("am", animated: animated)
        default: //PM
            self.tableView3.setCurrentTextTo("pm", animated: animated)
        }
    }
}
